import axios from "axios";
// import store from "@/store";
import { useRouter, useRoute } from "vue-router"
const baseURL = ""
const service = axios.create({
    baseURL,
    timeout: 20000,
});
// 发起请求之前的拦截器
import { getToken, getLang } from "@/utils/util";
import i18n from "@/lang"
import { Toast } from "vant";
const $t = i18n.global.t

service.interceptors.request.use(
    (config) => {
        // 如果有token 就携带tokon
        if (config.headers.type == 2) {
            config.headers['Content-Type'] = "application/json;charset=UTF-8"
            delete config.headers.type
        }
        
        config.headers.lang = getLang()
        const token = getToken();
        if (token) {
            config.headers.common.Authorization = token;
        }
        if (Object.prototype.hasOwnProperty.call(config, "token")) {
            config.headers.common.Authorization = config.token;
        }
        return config;
    },
    (error) => Promise.reject(error)
);


// 响应拦截器
service.interceptors.response.use(
    (response) => {
        const res = response.data;
        console.log('这个是返回的结果哦', res)
        if (res.code == 401) {
            console.log("进来界面了哦++++++++++++++++++++++++++++++++++++")
            sessionStorage.removeItem("_token")
            window.location.href = "#/login"

            Toast($t('two.loginOut'));
            return res
        }
        if (response.status !== 200) {
            return Promise.reject(res);
        } else if (res.code != 200) {
            console.log("错误提示", res)
            Toast(res.msg);
            Promise.reject(res);
            return res
        } else {
            return res;
        }
    },
    (error) => {
        Toast($t('two.conFail'));
        return Promise.reject(error);
    }
);
export default service;
